// membershipStore.ts - 会员卡管理工具
import { ref, computed } from 'vue'
import { getCurrentUser, getUserId } from './userStore'
import { getMemberPoints } from './pointsStore' // 改为使用会员积分

// 会员等级接口
export interface MemberLevel {
  level: number
  name: string
  pointsRequired: number
  benefits: string[]
  color: string
  icon: string
}

// 会员等级配置
const memberLevels: MemberLevel[] = [
  {
    level: 1,
    name: '运动新手',
    pointsRequired: 0,
    benefits: ['基础积分奖励', '新手专享优惠券'],
    color: '#95a5a6',
    icon: '🌱'
  },
  {
    level: 2,
    name: '健身爱好者',
    pointsRequired: 100,
    benefits: ['1.2倍积分奖励', '专属优惠券', '优先客服'],
    color: '#3498db',
    icon: '💪'
  },
  {
    level: 3,
    name: '运动达人',
    pointsRequired: 500,
    benefits: ['1.5倍积分奖励', '高级优惠券', '专属活动'],
    color: '#e74c3c',
    icon: '🏃'
  },
  {
    level: 4,
    name: '健身专家',
    pointsRequired: 1000,
    benefits: ['2倍积分奖励', '顶级优惠券', '专属客服', '免费课程'],
    color: '#f39c12',
    icon: '🏆'
  },
  {
    level: 5,
    name: '运动大师',
    pointsRequired: 2000,
    benefits: ['3倍积分奖励', '所有特权', '专属定制', '优先体验'],
    color: '#9b59b6',
    icon: '👑'
  }
]

// 获取用户当前会员等级
export function getCurrentMemberLevel(): MemberLevel {
  const points = getMemberPoints() // 使用会员积分判断等级
  
  // 从高到低查找符合条件的等级
  for (let i = memberLevels.length - 1; i >= 0; i--) {
    if (points >= memberLevels[i].pointsRequired) {
      return memberLevels[i]
    }
  }
  
  return memberLevels[0] // 默认返回最低等级
}

// 获取升级进度
export function getUpgradeProgress(): { current: number; required: number; percentage: number } {
  const points = getMemberPoints() // 使用会员积分
  const currentLevel = getCurrentMemberLevel()
  const nextLevel = getNextMemberLevel()
  
  if (!nextLevel) {
    return { current: points, required: points, percentage: 100 }
  }
  
  const current = points - currentLevel.pointsRequired
  const required = nextLevel.pointsRequired - currentLevel.pointsRequired
  const percentage = Math.min((current / required) * 100, 100)
  
  return { current, required, percentage }
}

// 获取下一个会员等级
export function getNextMemberLevel(): MemberLevel | null {
  const currentLevel = getCurrentMemberLevel()
  const nextLevelIndex = memberLevels.findIndex(level => level.level === currentLevel.level) + 1
  
  if (nextLevelIndex < memberLevels.length) {
    return memberLevels[nextLevelIndex]
  }
  
  return null // 已经是最高等级
}

// 获取所有会员等级
export function getAllMemberLevels(): MemberLevel[] {
  return memberLevels
}

// 获取积分倍率
export function getPointsMultiplier(): number {
  const currentLevel = getCurrentMemberLevel()
  
  switch (currentLevel.level) {
    case 1: return 1.0
    case 2: return 1.2
    case 3: return 1.5
    case 4: return 2.0
    case 5: return 3.0
    default: return 1.0
  }
}

// 会员卡信息
export function getMembershipCard() {
  const user = getCurrentUser()
  const currentLevel = getCurrentMemberLevel()
  const nextLevel = getNextMemberLevel()
  const progress = getUpgradeProgress()
  const points = getMemberPoints() // 显示会员积分
  
  return {
    user: {
      name: user.name || '默认用户',
      avatar: user.img || user.avatar,
      id: user.id
    },
    currentLevel,
    nextLevel,
    progress,
    points, // 会员积分，只增不减
    multiplier: getPointsMultiplier(),
    memberSince: user.createTime || new Date().toISOString().slice(0, 10)
  }
}